package secondriver.springsubway.example.jpa;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.PostPersist;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "person")
@EntityListeners(value={PersonListener.class})
public class Person implements Serializable {

	private static final long serialVersionUID = -7555669554776765422L;

	@Id
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "person_generator")
	@TableGenerator(name = "person_generator", table = "_gen_", allocationSize = 20, pkColumnName = "key_name", valueColumnName = "key_value", pkColumnValue = "person_id")
	private Integer id;

	private String name;

	@Temporal(TemporalType.DATE)
	private Date birthday;

	@Enumerated(EnumType.STRING)
	private PersonType type;

	@Embedded
	@AttributeOverrides({
			@AttributeOverride(name = "zip", column = @Column(name = "zip_code")),
			@AttributeOverride(name = "street", column = @Column(name = "street_name")) })
	private Address address;

	@OneToMany(mappedBy = "person",cascade={CascadeType.PERSIST,CascadeType.REMOVE})
	private List<Phone> phoneList = new ArrayList<Phone>();

	
	//Start
	/**
	 * Entity Inner Callback
	 */
	@PrePersist
	public void prePersist() {
		System.out.println("@PrePersist");
	}

	@PostPersist
	public void postPersist() {
		System.out.println("@PostPersist");
	}
	//End
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public List<Phone> getPhoneList() {
		return phoneList;
	}

	public void setPhoneList(List<Phone> phoneList) {
		this.phoneList = phoneList;
	}

	public PersonType getType() {
		return type;
	}

	public void setType(PersonType type) {
		this.type = type;
	}

	@Override
	public String toString() {
		return "Person [id=" + id + ", name=" + name + ", birthday=" + birthday
				+ ", type=" + type + ", address=" + address + ", phoneList="
				+ phoneList + "]";
	}
}
